package com.mall.manager.service.imp;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.manager.dao.TbItemDescMapper;
import com.mall.manager.dao.TbItemMapper;
import com.mall.manager.pojo.TbItem;
import com.mall.manager.pojo.TbItemDesc;
import com.mall.manager.pojo.TbItemExample;
import com.mall.manager.service.ItemService;
import com.mall.pojo.EasyUiPageBean;
import com.mall.pojo.TaotaoResult;
import com.mall.utils.IDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Date;
import java.util.List;


/**
 * @author Administrator
 */
@Service
@Component
@Transactional
public class ItemServiceImp implements ItemService {

    @Autowired
    TbItemMapper itemMapper;

    @Autowired
    TbItemDescMapper itemDescMapper;

    @Override
    public TbItem getItemById(long id) {
        TbItemExample example = new TbItemExample();
        TbItemExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(id);
        List<TbItem> tbItems = itemMapper.selectByExample(example);
        if (tbItems.size()>0){
            return tbItems.get(0);
        }else {
            return null;
        }
    }

    @Override
    public EasyUiPageBean listItems(int page, int rows) {
        PageHelper.startPage(page,rows);

        TbItemExample example = new TbItemExample();
        List<TbItem> tbItems = itemMapper.selectByExample(example);

        /*设置分页查询结果*/
        EasyUiPageBean result = new EasyUiPageBean();
        result.setRows(tbItems);

        /*设置总条数*/
        PageInfo<TbItem> pageInfo = new PageInfo<>(tbItems);
        long total = pageInfo.getTotal();
        result.setTotal(total);
        return result;
    }

    @Override
    public TaotaoResult addItem(TbItem item, String desc) {
        long itemId = IDUtils.genItemId();
        //补全属性
        item.setId(itemId);
        Date date = new Date();
        //1表示上架
        item.setStatus((byte) 1);
        item.setCreated(date);
        item.setUpdated(date);
        itemMapper.insert(item);

        //补全商品描述表
        TbItemDesc tbItemDesc = new TbItemDesc();
        tbItemDesc.setItemId(itemId);
        tbItemDesc.setCreated(date);
        tbItemDesc.setUpdated(date);
        tbItemDesc.setItemDesc(desc);

        itemDescMapper.insert(tbItemDesc);

        return TaotaoResult.ok(item);
    }

    @Override
    public TaotaoResult deleteItem(Long[] ids) {
        try {
            for (Long id : ids) {
                itemMapper.deleteByPrimaryKey(id);
            }
            return TaotaoResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return TaotaoResult.build(500,"删除商品失败!");
        }
    }

    @Override
    public TbItemDesc getItemDescById(long id) {
        TbItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(id);
        return itemDesc;
    }

    @Override
    public void update(TbItem newItem) {
        TbItem oldItem = itemMapper.selectByPrimaryKey(newItem.getId());
        newItem.setUpdated(new Date());
        newItem.setCreated(oldItem.getCreated());
        newItem.setCid(oldItem.getCid());
        newItem.setStatus(oldItem.getStatus());
        itemMapper.updateByPrimaryKey(newItem);
    }

}
